/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.delaphone.flocash.web.action;

import com.delaphone.flocash.web.bean.UserProfile;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;

/**
 *
 * @author tamdx
 */
public class ChangePassAction extends BaseAction {

    private UserService userService;
    private String old_password;
    private String new_password;

    @Override
    public String execute() {
        if (user == null) {
            return Action.ERROR;
        }
        return Action.SUCCESS;

    }

    public String doChangePass() {
        validateFields();
        if (user == null) {
            return Action.ERROR;
        }
        if (getFieldErrors().size() > 0) {
            addActionError(getText("user.security.empty"));
        } else if (!userService.changePassword(user.getEmail(), old_password, new_password)) {
            addActionError(getText("user.security.incorrectpass"));
        } else {
            request.setAttribute("msg", getText("user.security.success"));
        }
        return Action.INPUT;
    }

// <editor-fold defaultstate="collapsed" desc="set and get method">
    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getNew_password() {
        return new_password;
    }

    public void setNew_password(String new_password) {
        this.new_password = new_password;
    }

    public String getOld_password() {
        return old_password;
    }

    public void setOld_password(String old_password) {
        this.old_password = old_password;
    }// </editor-fold>

    public void validateFields() {
        if (Validate.isEmptyString(old_password)) {
            addFieldError("old_password", "*");
        }
        if (Validate.isEmptyString(new_password)) {
            addFieldError("new_password", "*");
        }

    }
}
